<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名">
                <a-input placeholder="请输入用户名" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="等级">
                <a-select v-model="queryParam.level_id">
                  <a-select-option :value="0"> 全部等级 </a-select-option>
                  <a-select-option :value="item.id" v-for="item in levelList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.order_no"></a-input>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="购买人">
                <a-input placeholder="请输入购买人" v-model="queryParam.buyer"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="下单时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="到账时间">
                <div class="select">
                  <a-range-picker
                    format="YYYY-MM-DD HH:mm"
                    valueFormat="YYYY-MM-DD HH:mm"
                    :placeholder="['开始时间', '结束时间']"
                    v-model="queryTime1"
                    @change="changeTime1"
                  />
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <a-table
        :rowKey="
          (r, i) => {
            return i
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.holder_avatar || record.holder_avatar_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.holder_name || record.holder_nickname }}</span>
            </div>
          </div>
        </div>
        <span slot="buyer_name" slot-scope="scope">
          {{ scope.buyer_name || scope.buyer_nickname }}
        </span>
        <span slot="type" slot-scope="text, scope">
          {{ showType(text, scope) }}
        </span>
        <span slot="pay_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="send_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="level_name" slot-scope="text">
          {{ text ? text : '-' }}
        </span>
        <span slot="pay_price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="money" slot-scope="text"> ￥{{ text || 0 }} </span>
        <div slot="action" slot-scope="text, scope">
          <div class="action">
            <a-button type="link" @click="editLevel(scope)" size="small">编辑</a-button>
            <span class="line"></span>
            <a-button type="link" @click="deleteHolder(scope)" size="small">删除</a-button>
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { allLevel, commission } from '@/api/shareholder'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '会员信息',
    scopedSlots: {
      customRender: 'name',
    },
    width: 200,
  },
  {
    title: '会员等级',
    dataIndex: 'level_name',
    scopedSlots: {
      customRender: 'level_name',
    },
  },
  {
    title: '佣金类型',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'type',
    },
  },
  {
    title: '佣金金额',
    dataIndex: 'money',
    scopedSlots: {
      customRender: 'money',
    },
  },
  {
    title: '订单号',
    dataIndex: 'order_no',
  },
  {
    title: '购买人',
    scopedSlots: {
      customRender: 'buyer_name',
    },
  },
  {
    title: '订单金额',
    dataIndex: 'pay_price',
    scopedSlots: {
      customRender: 'pay_price',
    },
  },
  {
    title: '下单时间',
    dataIndex: 'pay_time',
    scopedSlots: {
      customRender: 'pay_time',
    },
  },
  {
    title: '到账时间',
    dataIndex: 'send_time',
    scopedSlots: {
      customRender: 'send_time',
    },
  },
]
export default {
  props: {
    order: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      // 搜索参数
      queryParam: {
        order_id: 0,
        order_no: null,
        name: null,
        buyer: null,
        level_id: null,
        pay_start_time: null,
        pay_end_time: null,
        send_start_time: null,
        send_end_time: null,
        pageNo: 1,
        pageSize: 10,
        type: 1,
      },
      // 请求的数据列表
      list: [],
      // 表格列配置
      columns,
      // 数据条数
      totalCount: 0,
      // 所有等级列表
      levelList: [],
      // 下单时间
      queryTime: [],
      // 到账时间
      queryTime1: [],
    }
  },
  methods: {
    showType(type, scope) {
      if (scope.profit_name) {
        return scope.profit_name
      }
      let name = '其他'
      switch (parseInt(type)) {
        case 1:
          name = '股东分红'
          break
        case 2:
          name = '级差分红'
          break
        case 3:
          name = '区域分红'
          break
        case 4:
          name = '基础分销'
          break
        case 6:
          name = '直推出局团队收益'
          break
        case 7:
          name = '级差分红(原关系)'
          break
        case 8:
          name = '直推出局团队收益(原关系)'
          break
        case 9:
          name = '冻结金额释放'
          break
      }
      return name
    },
    // 获取数据列表
    async getList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await commission(this.queryParam)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.list = res.data.data
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 获取所有等级列表
    async allLevel() {
      const res = await allLevel()
      if (res.code == 0) {
        this.levelList = res.data
      }
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.pay_start_time = e[0]
        this.queryParam.pay_end_time = e[1]
      } else {
        this.queryParam.pay_start_time = null
        this.queryParam.pay_end_time = null
      }
    },
    // 切换筛选时间
    changeTime1(e) {
      if (e.length) {
        this.queryParam.send_start_time = e[0]
        this.queryParam.send_end_time = e[1]
      } else {
        this.queryParam.send_start_time = null
        this.queryParam.send_end_time = null
      }
    },
  },
  mounted() {
    if (this.$route.query.order_id) {
      this.queryParam.order_id = this.$route.query.order_id
    }
    if (this.$route.query.profit_type) {
      this.queryParam.profit_type = this.$route.query.profit_type
    }
    console.log(this.queryParam)
    this.allLevel()
    this.getList()
  },
}
</script>

<style lang="less" scoped>
.search {
  /deep/ .ant-card-body {
    padding-top: 0 !important;
    padding-bottom: 14px !important;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
.level_one_count {
  text-decoration: underline;
  cursor: pointer;
  color: #ff4400;
}
</style>
